package com.briup.jz.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.briup.jz.bean.BaseRolePrivilege;
import com.briup.jz.bean.BaseRolePrivilegeExample;
import com.briup.jz.dao.BaseRolePrivilegeMapper;
import com.briup.jz.service.IBaseRolePrivilegeService;
import com.briup.jz.utils.CustomerException;

@Service
public class BaseRolePrivilegeServiceImpl implements IBaseRolePrivilegeService {

    @Autowired
    private BaseRolePrivilegeMapper baseRolePrivilegeMapper;

    // 保存或者更新操作
    @Override
    public void saveOrUpdate(BaseRolePrivilege baseRolePrivilege) throws CustomerException {
        //根据i的查询数据库，如果结果为空，就为插入，否则为更新
        BaseRolePrivilege rolePrivilege = baseRolePrivilegeMapper.selectByPrimaryKey(baseRolePrivilege.getId());

        if (rolePrivilege != null) {
            //更新
            baseRolePrivilegeMapper.updateByPrimaryKey(baseRolePrivilege);
        } else {

            baseRolePrivilegeMapper.insert(baseRolePrivilege);
        }


//        BaseUserRole userRole = baseUserRoleMapper.selectByPrimaryKey(baseUserRole.getId());
//        if (userRole != null) {
//            //更新
//            baseUserRoleMapper.updateByPrimaryKey(baseUserRole);
//        } else {
//            //插入
//            baseUserRoleMapper.insert(baseUserRole);
//        }

    }

    // 根据id 查询
    @Override
    public List<BaseRolePrivilege> query(Long id) {
        BaseRolePrivilegeExample example = new BaseRolePrivilegeExample();

        if (id != null) {

            example.createCriteria().andIdEqualTo(id);
        }
        List<BaseRolePrivilege> list = baseRolePrivilegeMapper.selectByExample(example);

        return list;
    }

    // 根据id 进行删除
    @Override
    public void deleteById(long id) throws CustomerException {

        BaseRolePrivilege baseRolePrivilege = baseRolePrivilegeMapper.selectByPrimaryKey(id);

        if (baseRolePrivilege == null) {

            throw new CustomerException("要删除的分类信息不存在");
        }
        baseRolePrivilegeMapper.deleteByPrimaryKey(id);
    }

}
